<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>

<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<%@ page import="java.util.Date"%>
<% String formId = "form_" + new Date().getTime();
   String gridId = "grid_" + new Date().getTime();
   String path = request.getContextPath()+request.getServletPath().substring(0,request.getServletPath().lastIndexOf("/")+1);
%>
<script type="text/javascript">
$(function (){
	
	var contents_dep9 = [{title:'请选择', value: ''}];
	var contents_dep10 = [{title:'请选择', value: ''}];
	var grid = $('[data-role="drgsVersionGrid"]');
    var form = $("[name='drgsVersionForm']");
    
    //$("#addDateID").val(new Date().getFullYear() - 1 + '-01-01');
    $("#addDateID").datetimepicker({
		format : "yyyy-mm-dd",
		autoclose : true,
		todayBtn : true,
		minView : 2,
		language : 'zh-CN'
	});
	PageLoader = {
	    initSearchPanel:function(){
	    	initeICDTypeList("addICD10TypeID","addICD9TypeID",form) ;
	    },
	    initGridPanel: function(){
	         var self = this;
	         var width = 152;
	        
	         return grid.grid({
	                identity:"verCode",
	                buttons: [
	                        {content: '<button class="btn btn-primary" type="button"><span class="glyphicon glyphicon-plus"><span>添加</button>', action: 'add'},
	                        {content: '<button class="btn btn-success" type="button"><span class="glyphicon glyphicon-edit"><span>修改</button>', action: 'modify'},
	                        {content: '<button class="btn btn-danger" type="button"><span class="glyphicon glyphicon-remove"><span>删除</button>', action: 'delete'}
	                    ],
	                url:"${pageContext.request.contextPath}/DrgsVersionManage/pageJson.dhc",
	                columns: 
                	[
                	 { title: '版本代码', 
                		 name: 'verCode',
                		 width: '16.66%',
                	 },
                	 { title: '版本描述', 
                	   name: 'verDesc', 
                	   width: '16.66%'},
     	             { title: 'ICD9版本', 
                	   name: 'icd9Version', 
                	   width: '16.66%',
                	   render: function(item,n,name){
              			 return getDicDesc('OPER_ICD_VERSION',item['icd9Version'])
              		    }
                	 },
     	             { title: 'ICD10版本', 
                	   name: 'icd10Version', 
                	   width: '16.66%',
                	   render: function(item,n,name){
                		return getDicDesc('DIA_ICD_VERSION',item['icd10Version'])
                  		 }
                	   },
     	             { title: '地区代码', 
                		   name: 'regionCode', 
                		   width: '16.66%'},
     	             { title: '添加日期', 
                			   name: 'addDate', 
                			   width: '16.66%'}
	                ]
	         }).on({
	                   'add': function(){
	                       self.add($(this));
	                   },
	                   'modify': function(event, data){
	                        var indexs = data.data;
	                        var $this = $(this);
	                        if(indexs.length == 0){
	                            drgUI.warning('请选择一条记录进行修改');
	                            return;
	                        }
	                        if(indexs.length > 1){
	                             drgUI.warning('只能选择一条记录进行修改');
	                            return;
	                        }
	                       self.modify(data.item[0],indexs[0], $this);
	                    },
	                   'delete': function(event, data){
	                        var indexs = data.data;
	                        var $this = $(this);
	                        if(indexs.length == 0){
	                            drgUI.warning('请选择要删除的记录！');
	                            return;
	                        }
	                        var remove = function(){
	                            self.remove(indexs, $this);
	                        };
	                        drgUI.confirm({
	                            content: '确定要删除所选记录吗?',
	                            callSure: remove
	                        });
	                   }
	         });
	         
	    },
	    add: function(grid){
	        var self = this;
	        var dialog = $('<div class="modal fade"><div class="modal-dialog">'
	        	+'<div class="modal-content"><div class="modal-header"><button type="button" class="close" '
	        	+'data-dismiss="modal" aria-hidden="true">&times;</button>'
	        	+'<h4 class="modal-title">新增</h4></div><div class="modal-body">'
	        	+'<p>One fine body&hellip;</p></div><div class="modal-footer">'
	        	+'<button type="button" class="btn btn-default" data-dismiss="modal">取消</button>'
	        	+'<button type="button" class="btn btn-success" id="save">保存</button></div></div>'
	        	+'</div></div>');
	        $.get('<%=path%>/drgsVersionManage-add.jsp').done(function(html){
	            dialog.modal({
	                keyboard:false
	            }).on({
	                'hidden.bs.modal': function(){
	                    $(this).remove();
	                }
	            }).find('.modal-body').html(html);
	            self.initPage(dialog.find('form'),"");
	        });
	        dialog.find('#save').on('click',{grid: grid}, function(e){
	        	if(!Validator.Validate(dialog.find('form')[0],3))return;
	              var verCode = dialog.find('#verCodeID').val();
	            	  $.post('${pageContext.request.contextPath}/DrgsVersionManage/isVerOrderExist.dhc',{'verCode':verCode}).done(function(result){
	            		  if(result.verCode) {
			        		  drgUI.error('版本编码已存在，请重新输入!');
			        	  }else {
			        		  var year=new Date().getFullYear();
			      	    	var month=new Date().getMonth()+1;
			      	    	var date=new Date().getDate();
			      	    	var addDate=year+'-'+month+'-'+date;
	                			 $.post('${pageContext.request.contextPath}/DrgsVersionManage/add.dhc?addDate='+addDate,dialog.find('form').serialize()).done(function(result){
	                				
	                				 if(result.success ){
	      	  	                        dialog.modal('hide');
	      	  	                        e.data.grid.data('koala.grid').refresh();
	      	  	                        drgUI.success('保存成功！');
	      	  	                    }else{
	      	  	                       drgUI.error('保存失败,'+result.errorMessage);
	      	  	                     }
	      	  	              	});
	                		}
	            	  });
	        });
	    },
	    modify: function(item,id, grid){
	        var self = this;
	        var dialog = $('<div class="modal fade"><div class="modal-dialog"><div class="modal-content"><div class="modal-header"><button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button><h4 class="modal-title">修改</h4></div><div class="modal-body"><p>One fine body&hellip;</p></div><div class="modal-footer"><button type="button" class="btn btn-default" data-dismiss="modal">取消</button><button type="button" class="btn btn-success" id="save">保存</button></div></div></div></div>');
	        $.get('<%=path%>/drgsVersionManage-update.jsp').done(function(html){
	               dialog.find('.modal-body').html(html);
	               self.initPage(dialog.find('form'),item);
	               dialog.find("#verCodeIDUpdate").val(item.verCode);
	               dialog.find("#verDescIDUpdate").val(item.verDesc);
	               dialog.find("#icd9VersionIDUpdate").val(item.icd9Version);
	               dialog.find("#icd10VersionIDUpdate").val(item.icd10Version);
	               dialog.find("#regionCodeIDUpdate").val(item.regionCode);
	                dialog.modal({
	                    keyboard:false
	                }).on({
	                    'hidden.bs.modal': function(){
	                        $(this).remove();
	                    }
	                });
	                dialog.find('#save').on('click',{grid: grid}, function(e){
	                    if(!Validator.Validate(dialog.find('form')[0],3))return;
	                    var verCode = item.verCode;
	                    $.post('${pageContext.request.contextPath}/DrgsVersionManage/isVerOrderExist.dhc',{'verCode':verCode}).done(function(result){
		            		  if(result){
		                			 $.post('${pageContext.request.contextPath}/DrgsVersionManage/update.dhc?addDate='+item.addDate, dialog.find('form').serialize()).done(function(result){
		     	                        if(result.success){
		     	                            dialog.modal('hide');
		     	                            e.data.grid.data('koala.grid').refresh();
		     	                            drgUI.success('保存成功！');
		     	                        }else{
		     	                            drgUI.error(result.actionError);
		     	                        }
		     	                    });
		                		}
	                    });
	                });
	        });
	    },
	    initPage: function(form,data){
	               form.find('.select').each(function(){
	                    var select = $(this);
	                    var idAttr = select.attr('id');//获取id的值
	                    var selectID;
	                    var content10 = [] ;
	                    var content9 = [] ;
	                    var icd9VersionUpdate=getDicDesc('OPER_ICD_VERSION',data['icd9Version']);
	                    var icd10VersionUpdate=getDicDesc('DIA_ICD_VERSION',data['icd10Version']);
	                     if( idAttr.indexOf("TypeID") > 0 ) {//indexof("abc");查找abc
	                    	content10=contents_dep10;
	                    	content9=contents_dep9;
	                    }  
	                    if(idAttr=="addICD10TypeID"){
	                    	
	                    	selectID=select.select({
		                        title: '请选择',
		                        contents: content10
		                    });
	                    }else if(idAttr=="addICD9TypeID"){
	                    	selectID=select.select({
		                        title: '请选择',
		                        contents: content9
		                    });
	                    }else if(idAttr=="updateICD10TypeID"){
                           if(data.icd10Version!=null){
                        	  
	                    	selectID=select.select({
		                        title: icd10VersionUpdate,
		                        contents: content10
		                    });
                        	  form.find("#updateICD10TypeID_").val(data.icd10Version);
                           }else if(data.icd10Version==null){
                        	   selectID=select.select({
   		                        title: '请选择',
   		                        contents: content10
   		                    });
                           }
	                    }else if(idAttr=="updateICD9TypeID"){
	                    	if(data.icd9Version!=null){
	                    	selectID=select.select({
		                        title: icd9VersionUpdate,
		                        contents: content9
		                    });
	                    	
	                    	form.find("#updateICD9TypeID_").val(data.icd9Version);
	                    	}else if(data.icd9Version==null){
	                        	   selectID=select.select({
	      		                        title: '请选择',
	      		                        contents: content9
	      		             });
	                         }
	                    }
	                    selectID.on('change', function(){
	                    	 var value=$(this).getValue();
	                        form.find('#'+ idAttr + '_').val($(this).getValue());
	                        if(idAttr == "addICD10TypeID" ) {
	                        	// 切换时下级下拉列表赋空值
	                        	if(value != "") {
	                        		//form.find("#addICD10TypeID").setValue(value);
	                        		form.find("#addICD10TypeID_").setValue(value);
	                        	}
	                        	
	                            } else if( idAttr == "updateICD10TypeID" ) {
	                        	if(value != "") {
	                        		//form.find("#updateICD10TypeID").setValue(value);
	                        		form.find("#updateICD10TypeID_").setValue(value);
	                        	}
	                            }else if(idAttr == "addICD9TypeID" ) {
	                        	// 切换时下级下拉列表赋空值
	                        	if(value != "") {
	                        		//form.find("#addICD9TypeID").setValue(value);
	                        		form.find("#addICD9TypeID_").setValue(value);
	                        	}
	                            }else if( idAttr == "updateICD9TypeID" ) {
	                        	if(value != "") {
	                        		//form.find("#updateICD9TypeID").setValue(value);
	                        		form.find("#updateICD9TypeID_").setValue(value);
	                        	}
	                        }
	                    }); 
	                    
	               });
	    },
	    
	    remove: function(ids, grid){
	    	var data = [{ name: 'verCodes', value: ids.join(',') }];
	    	$.post('${pageContext.request.contextPath}/DrgsVersionManage/delete.dhc', data).done(function(result){
	                        if(result.success){
	                            grid.data('koala.grid').refresh();
	                            drgUI.success('删除成功');
	                        }else{
	                            drgUI.error('error');
	                        }
	    	});
	    }
	}
	
	function initeICDTypeList(selectId10,selectId9,form) {
		
		$.each(diction.DIA_ICD_VERSION,function(i,n) {
			contents_dep10.push({title:n.DICDESC, value:n.DICVALUE});
		 });
		 $.each(diction.OPER_ICD_VERSION,function(i,n) {
			 contents_dep9.push({title:n.DICDESC, value:n.DICVALUE});
		 });
		 if(selectId10!=''){
	            if(selectId10=='addICD10TypeID'||selectId10=='updateICD10TypeID'){
	            form.find('#'+selectId10).select({
			        title: '请选择' ,
			        contents: contents_dep10
			    });
	            }
	            } 
	            if(selectId9!=''){
	           if(selectId9=='addICD9TypeID'||selectId9=='updateICD9TypeID'){
	            	 form.find('#'+selectId9).select({
					        title: '请选择' ,
					        contents: contents_dep9
					    });
	            }
	            }
	}
	
	
	
	PageLoader.initSearchPanel();
	PageLoader.initGridPanel();
	form.find('#search').on('click', function(){
            if(!Validator.Validate(document.getElementById("<%=formId%>"),3))return;
            var params = {};
            form.find('input').each(function(){
                var $this = $(this);
                var name = $this.attr('name');
                if(name){
                    params[name] = $this.val();
                }
            });
            grid.getGrid().search(params);
        });
});
</script>
</head>
<body>
<div style="width:98%;margin-right:auto; margin-left:auto; padding-top: 15px;">
<!-- search form -->
<%-- <form name="drgsVersionForm" id=<%=formId%> target="_self" class="form-horizontal">
<table border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td>
    	<div class="form-group">
    		<label class="control-label" style="width:100px;float:left;">版本编码:&nbsp;</label>
            <div style="margin-left:15px;float:left;">
            	<input name="verCode" class="form-control" type="text" style="width:160px;" id="verCodeID"  />
			</div>
    		<label class="control-label" style="width:100px;float:left;">版本描述:&nbsp;</label>
            <div style="margin-left:15px;float:left;">
            	<input name="verDesc" class="form-control" type="text" style="width:160px;" id="verDescID"  />
			</div>
            <label class="control-label" style="width:100px;float:left;">地区代码:&nbsp;</label>
            <div style="margin-left:15px;float:left;">
            	<input name="regionCode" class="form-control" type="text" style="width:160px;" id="regionCodeID"  />
        	</div>
        	<label class="control-label" style="width:100px;float:left;">添加日期:&nbsp;</label>
            <div style="margin-left:15px;float:left;">
	            <input name="addDate" class="form-control" type="text" style="width:160px;" id="addDateID"  />
	        </div>
		</div>
	</td>
	<td style="vertical-align: bottom;">
	<button id="search" type="button" style="position:relative; margin-left:35px; top: -15px" class="btn btn-primary">
	<span class="glyphicon glyphicon-search"></span>
	&nbsp;查询</button></td>
  </tr>
</table>	
</form> --%>
<form name="drgsVersionForm" id=<%=formId%> target="_self" class="form-horizontal">
	<div class="container-fluid">
		<div class="row">
			<div class="form-group col-md-3">
				<label for="verCode">版本编码:&nbsp;</label>
	            <input name="verCode" class="form-control" type="text" style="width:160px;" id="verCodeID"  />
			</div>
			<div class="form-group col-md-3">
				<label for="verDesc">版本描述:&nbsp;</label>
	            <input name="verDesc" class="form-control" type="text" style="width:160px;" id="verDescID"  />
			</div>
			<div class="form-group col-md-3">
				<label for="regionCode">地区代码:&nbsp;</label>
	            <input name="regionCode" class="form-control" type="text" style="width:160px;" id="regionCodeID"  />
			</div>
			<div class="form-group col-md-3">
				<label for="addDate">添加日期:&nbsp;</label>
	            <input name="addDate" class="form-control" type="text" style="width:160px;" id="addDateID"  />
			</div>
			<div class="form-group col-md-1">
				<button id="search" type="button"  class="btn btn-primary"><span class="glyphicon glyphicon-search"></span>&nbsp;查询</button>
			</div>
			
		</div>
	</div>
</form>
<!-- grid -->
<div data-role="drgsVersionGrid" id=<%=gridId%>></div>
</div>
</body>
</html>